﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using FNSShoppingCart;
public partial class Pages_WebControls_ProductsMini : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private int productID;
    private string productName;
    //  private int rating;
    //private int viewed;

    public string ProductID
    {
        get { return ViewState["productID"] as string; }//productID + ""; }
        set
        {
            productID = Int32.Parse(value);
            ViewState["productID"] = productID;//Convert.ToInt32(value);

            if (!String.IsNullOrEmpty(productName))
            {
                UrlLink();
            }
        }
    }

    public string ProductName
    {
        get { return _LinkName.Text; }
        set
        {
            if (value.Length > 30)
            {
                _LinkName.Text = value.Substring(0, 28) + "...";
            }
            else { _LinkName.Text = value; }
            _LinkName.ToolTip = value;
            productName = value;
            if (productID != 0)
            {
                UrlLink();
            }
        }
    }


    public string ImageUrl
    {
        get { return _Image.ImageUrl; }
        set
        {
            if (value.Length > 0)
            {
                _Image.ImageUrl = Page.ResolveUrl(value);
            }
            else
            {
                _Image.ImageUrl = Page.ResolveUrl("~/Content/SiteImages/NoPictre.png");
            }
        }
    }


    public decimal Price
    {
        //get { return _Price.Text; }
        set { _Price.Text = String.Format("{0:0,0.##} RON", value); }
    }


    public decimal OldPrice
    {
        //get { return _OldPrice.Text; }
        set
        {
            var oldprice = value;
            if (oldprice != 0)
            {
                _OldPrice.Text = String.Format("{0:0,0.##} RON", value);
            }
            else
            {
                _OldPrice.Text = "";
            }
        }
    }
    public string Rating
    {
        get { return ViewState["rating"] as string; }
        set { ViewState["rating"] = value; }
    }
    public string Views
    {

        set
        {
            _Views.ImageUrl = Page.ResolveUrl("~/Content/SiteImages/eye.png");
            _Views.ToolTip = "Produs vizualiat de " + value + " ori";
        }
    }

    public bool IsPromotion
    {
        set
        {
            if (value)
            {
                _IsPromotion.ImageUrl = Page.ResolveUrl("~/Content/SiteImages/promotie.gif");
                _IsPromotion.ToolTip = "Promotie!";
                _IsPromotion.Visible = true;
            }
        }
    }


    private void UrlLink()
    {
        var url = Page.GetRouteUrl("ProductDetails", new { id = productID, prodName = productName }).Replace("%20","_");
        _LinkImage.NavigateUrl = url ;
        _LinkName.NavigateUrl = url;
        _LinkDetails.NavigateUrl = url;
    }


    public string GetRatingImage()
    {
        string rating_icon;
        switch (ViewState["rating"] as string)
        {
            case "0": rating_icon = Page.ResolveUrl("~/Content/SiteImages/rating0.png"); break;
            case "1": rating_icon = Page.ResolveUrl("~/Content/SiteImages/rating1.png"); break;
            case "2": rating_icon = Page.ResolveUrl("~/Content/SiteImages/rating2.png"); break;
            case "3": rating_icon = Page.ResolveUrl("~/Content/SiteImages/rating3.png"); break;
            case "4": rating_icon = Page.ResolveUrl("~/Content/SiteImages/rating4.png"); break;
            default: rating_icon = Page.ResolveUrl("~/Content/SiteImages/rating1.png"); break;
        }
        return rating_icon;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int productID = Convert.ToInt32(ViewState["productID"]);
        MyShoppingCart cart = new MyShoppingCart();
        String cartID = cart.GetShoppingCartId();
        cart.AddItem(cartID, productID, 1);
    }
}